package nl.hu.verbrandendephoenix.controller;

import java.util.ArrayList;

import nl.hu.verbrandendephoenix.client.AppManager;
import nl.hu.verbrandendephoenix.client.NotificationManager;
import nl.hu.verbrandendephoenix.shared.Auction;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;

public class TopAuctions {

	private nl.hu.verbrandendephoenix.view.TopAuctions topAuctionsView;

	public TopAuctions() { 
		topAuctionsView = new nl.hu.verbrandendephoenix.view.TopAuctions();
	}

	public void getPanel() {
		RootPanel.get("content").clear();
		getTopAuctions();
	}
	
	public Panel getResultPanel(Grid g) {
		RootPanel.get("content").clear();
		Panel p = topAuctionsView.constructForm(g);
		return p;
	}

	private void getTopAuctions() {
		final NotificationManager nM =  NotificationManager.get();

		AppManager.get().getRpcService().getTopAuctions(new AsyncCallback<ArrayList<Auction>>() {

			public void onFailure(Throwable caught) {
				Window.alert("Communication Failure!");
			}

			public void onSuccess(ArrayList<Auction> result) {
				if(result.size() != 0) {
					RootPanel pnlContent = RootPanel.get("content");
					pnlContent.add(getResultPanel(topAuctionsView.printResult(result)));
				} 
				else {
					nM.createNotification("Geen veilingen gevonden.");
				}
			}
		});
	}
}
